<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/04/11 20:58:15 $)
// $RCSfile: admin_visitorbans.php,v $ : $Revision: 1.4 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

if (!$staffauth->isAdmin()) {
	trigger_error("You do not have permission to access $PHP_SELF", E_USER_ERROR);
}

require_once ("./modules/livesupport/functions_visitorbans.php");
require_once ("./includes/functions_html.php");

$template->loadLanguageSection("admin_livesupport");

/**
* ###############################################
* FUNCTION DECLARATIONS
* ###############################################
*/

/**
* Callback for visitor bans
*/
function _visitorBansFields($arg)
{
	global $_SWIFT;
	$record = &$arg[0];
	$fields = &$arg[1];

	$record["staffid"] = $_SWIFT["staffcache"][$record["staffid"]]["fullname"];
	$record["dateline"] = edate($_SWIFT["settings"]["dt_datetimeformat"], $record["dateline"]);

	$record["options"] = '<a href="index.php?_m=livesupport&_a=editvisitorban&visitorbanid='. $record["visitorbanid"] .'" title="'. $_SWIFT["language"]["edit"] .'"><img src="'. $_SWIFT["themepath"] .'icon_edit.gif" border="0">&nbsp;'. $_SWIFT["language"]["edit"] .'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a onClick="javascript:doConfirm(\''. $_SWIFT["language"]["actionconfirm"] .'\', \'index.php?_m=livesupport&_a=visitorbans&do=delete&visitorbanid='. $record["visitorbanid"] .'\');" href="#" title="'. $_SWIFT["language"]["delete"] .'"><img src="'. $_SWIFT["themepath"] .'icon_delete.gif" border="0">&nbsp;'. $_SWIFT["language"]["delete"] .'</a>&nbsp;';

	return $record;
}

/**
* Deletes the given visitor bans
*/
function _maDeleteVisitorBans()
{
	global $dbCore, $_SWIFT;

	if (is_array($_POST["itemid"]))
	{
		deleteVisitorBans($_POST["itemid"]);
	}
}


/**
* ###############################################
* VISITOR BANS
* ###############################################
*/
if ($eventaction == "visitorbans")
{
	$displaybox = false;

	if ($_GET["do"] == "delete" && !empty($_GET["visitorbanid"]))
	{
		$infomessage = $_SWIFT["language"]["ipdelconfirm"];

		deleteVisitorBans(array($_GET["visitorbanid"]));
	} else if ($_POST["do"] == "insert") {
		if (trim($_POST["ipaddress"]) == "" || !isValidIP($_POST["ipaddress"]))
		{
			$errormessage = $_SWIFT["language"]["invalidip"];
			$displaybox = true;
		} else {
			insertBan($_POST["ipaddress"]);

			printRedirect(sprintf($_SWIFT["language"]["ipinsertconfirm"], htmlspecialchars($_POST["ipaddress"])), "index.php?_m=livesupport&_a=visitorbans&ipinsert=".urlencode($_POST["ipaddress"]));
			exit;
		}
	}
	

	if (!empty($_REQUEST["ipinsert"]))
	{
		$infomessage = sprintf($_SWIFT["language"]["ipinsertconfirm"], htmlspecialchars($_REQUEST["ipinsert"]));
	} else if (!empty($_REQUEST["ipupdate"])) {
		$infomessage = sprintf($_SWIFT["language"]["ipupdateconfirm"], htmlspecialchars($_REQUEST["ipupdate"]));
	}

	$options["recordsperpage"] = "10";
	$options["sortby"] = "dateline";
	$options["sortorder"] = "desc";
	$options["massaction"][0]["title"] = $_SWIFT["language"]["delete"];
	$options["massaction"][0]["callback"] = "_maDeleteVisitorBans";

	$options["idname"] = "visitorbanid";
	$options["quicksearch"] = true;

	$options["advancedsearch"][0]["title"] = $_SWIFT["language"]["ipaddress"];
	$options["advancedsearch"][0]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'visitorbans` WHERE `ipaddress` LIKE \'%$_searchstr%\' $_sortjoin;';

	$options["callback"] = "_visitorBansFields";

	$options["toolbar"][0]["title"] = $_SWIFT["language"]["insert"];
	$options["toolbar"][0]["url"] = "javascript:switchDisplay('insertban');";

	$fields[0]["name"] = "ipaddress";
	$fields[0]["title"] = $_SWIFT["language"]["ipaddress"];
	$fields[0]["width"] = "";

	$fields[1]["name"] = "staffid";
	$fields[1]["title"] = $_SWIFT["language"]["addedby"];
	$fields[1]["width"] = "200";
	$fields[1]["align"] = "center";

	$fields[2]["name"] = "dateline";
	$fields[2]["title"] = $_SWIFT["language"]["bandate"];
	$fields[2]["width"] = "180";
	$fields[2]["align"] = "center";

	$fields[3]["type"] = "custom";
	$fields[3]["name"] = "options";
	$fields[3]["title"] = $_SWIFT["language"]["options"];
	$fields[3]["width"] = "150";
	$fields[3]["align"] = "center";

	$grid = new Grid();

	$interface->adminHeader($_SWIFT["language"]["livesupport"]." > ".$_SWIFT["language"]["visitorbans"], 1);

	$interface->adminNavBar($_SWIFT["language"]["visitorbans"], $_SWIFT["language"]["desc_visitorbans"] , 13);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	echo '<div id="insertban" style="DISPLAY:'. iif($displaybox, "block", "none") .';">'.SWIFT_CRLF;
	printFormStart();
	printMainTableHeader($_SWIFT["language"]["insertvisitorban"]);

	printTextRow("ipaddress", $_SWIFT["language"]["ipaddress"], $_SWIFT["language"]["desc_ipaddress"], "text", $_POST["ipaddress"]);

	printSubmitRow($_SWIFT["language"]["insert"]);

	printMainTableFooter();
	addHiddenField("_m", "livesupport");
	addHiddenField("_a", "visitorbans");
	addHiddenField("do", "insert");
	printFormEnd();
	echo '<BR /></div>'.SWIFT_CRLF;

	$grid->start("visitorbans", $_SWIFT["language"]["visitorbanlist"], 'SELECT * FROM `'. TABLE_PREFIX .'visitorbans` $_sortjoin;', 'SELECT COUNT(*) AS totalitems FROM `'. TABLE_PREFIX .'visitorbans`;', 'SELECT * FROM `'. TABLE_PREFIX .'visitorbans` WHERE `ipaddress` LIKE \'%$_searchstr%\' $_sortjoin;', $fields, $options);
	$grid->display("visitorbans");
	
	$interface->adminFooter();





/**
* ###############################################
* EDIT VISITOR BAN
* ###############################################
*/
} else if ($eventaction == "editvisitorban") {
	$_visitorban = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."visitorbans` WHERE `visitorbanid` = '". intval($_REQUEST["visitorbanid"]) ."';");
	if (empty($_visitorban["visitorbanid"]))
	{
		trigger_error($_SWIFT["language"]["invalidban"], E_USER_ERROR);
		exit;
	}

	if ($_POST["step"] == "1") {
		if (trim($_POST["ipaddress"]) == "" || !isValidIP($_POST["ipaddress"]))
		{
			$errormessage = $_SWIFT["language"]["invalidip"];
		} else {
			updateVisitorBan($_visitorban["visitorbanid"], $_POST["ipaddress"]);

			printRedirect(sprintf($_SWIFT["language"]["ipupdateconfirm"], htmlspecialchars($_POST["ipaddress"])), "index.php?_m=livesupport&_a=visitorbans&ipupdate=".urlencode($_POST["ipaddress"]));
			exit;
		}
	}
	
	$_POST = array_merge($_POST, $_visitorban);

	$interface->adminHeader($_SWIFT["language"]["livesupport"]." > ".$_SWIFT["language"]["visitorbans"], 1);

	$interface->adminNavBar($_SWIFT["language"]["visitorbans"], $_SWIFT["language"]["desc_visitorbans"] , 13);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	printFormStart();
	printMainTableHeader($_SWIFT["language"]["updatevisitorban"]);

	printTextRow("ipaddress", $_SWIFT["language"]["ipaddress"], $_SWIFT["language"]["desc_ipaddress"], "text", $_POST["ipaddress"]);

	printSubmitRow($_SWIFT["language"]["update"]);

	printMainTableFooter();
	addHiddenField("_m", "livesupport");
	addHiddenField("_a", "editvisitorban");
	addHiddenField("visitorbanid", $_visitorban["visitorbanid"]);
	addHiddenField("step", "1");
	printFormEnd();
	
	$template->assign("backurl", "index.php?_m=livesupport&_a=visitorbans");
	$interface->adminFooter();
}

?>